/**
 * @file: device.c
 * @brief: 底层的函数和数据结构
 * @author: liaowx
 * @date: 2019.07.30
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "device.h"

/* 桶排序 */	
int sort_bucket(const int src_size)
{
	printf("src_size : %d\n", src_size);
	int input_data;
	int index = 0;
	int *dst = NULL;
	int dst_length;
	
	dst_length = src_size * BUCKET_SIZE_PROPORTION;
	printf("You must input your data rang: 0 ~ %d \r\n", dst_length);

	dst = (int *)malloc(dst_length * sizeof(int));
	if (!dst) {
		return -BUCKET_MALLOC_ERR;
	}
	
	memset(dst, 0, dst_length);
	
	/* Check the input_data is correct. */
	for (index = 0; index < src_size; index++) {
		scanf("%d", &input_data);
		if ((input_data < dst_length) && (input_data > 0 )) {
			dst[input_data]++;	
		} else {
			printf("You must input your data rang: 0 ~ %d \r\n", dst_length);
			index--;
		}
	}
	getchar();

	for (index = 0; index < dst_length; index++) {
		if (dst[index] != 0) {
			printf("%d ", index);
		}
	}
	printf("\r\n");

	free(dst);
	printf("Algorithm: %s(): End...\r\n", __func__);
	
	return 0;
}

int sort_bubble(const int src_size)
{
	printf("Algorithm: %s(): End...\r\n", __func__);
	
	return 0;
}

struct core *get_dev(void)
{
	struct sort *sort[SORT_NUM];
	struct core sort_core[SORT_NUM];
	struct core *return_sort_core;

	sort[BUCKET] = (struct sort *)malloc(sizeof(struct sort));
    sort[BUCKET]->cmd = BUCKET;

    sort[BUBBLE] = (struct sort *)malloc(sizeof(struct sort));
    sort[BUBBLE]->cmd = BUBBLE;

    sort_core[BUCKET].cmd = sort[BUCKET]->cmd;
    sort_core[BUCKET].dev_ctl = sort_bucket;
    sort_core[BUCKET].ioctl = sort_ioctl;

    sort_core[BUBBLE].cmd = sort[BUBBLE]->cmd;
    sort_core[BUBBLE].dev_ctl = sort_bubble;
    sort_core[BUBBLE].ioctl = sort_ioctl;

	return_sort_core = sort_core;

	return return_sort_core;
}